<include file="common:header"/>
<link rel="stylesheet" href="{:C('__STATIC__')}{$theme}/Static/{:C('css')}/agentview.css" />
    <div id="header">
        <div class="header">
            <h1 class="tit">{$site_title}</h1>
            <!--<p class="goback">-->
                <!--<a class="return" href="javascript:history.go(-1);">返回</a>-->
            <!--</p>-->
            <!--<p class="more"><a class="share" href=""></a></p>-->
        </div>
    </div>
    <!--<form action="{:U('Agent/agent_add')}" method="post" id="agent_add_form">-->
        <div class="person-msg">
            <div class="person-top">
                <div class="login-name">
                    <p class="login">代理名称</p>
                    <input type="text" name="agent_name" class="form-input" id="agent_name"/>
                </div>
                <div class="login-name">
                    <p class="birthday">手机号</p>
                    <input type="text" name="mobile" class="form-input" id="mobile" maxlength="11"/>
                </div>

                <foreach name="agent_brands" item="ranks">
                    <notempty name="ranks['rank_id']">
                        <div class="login-name">
                            <p class="login">{$ranks['b_name']}</p>
                            <input type="hidden" class="hidden_brand" name="brand_id[]" value="{$ranks['brand_id']}"/>
                            <select name="rank_id[]" class="fleft ranks" style="width:auto;">
                                <empty name="ranks['ranks']">
                                    <option value="0">您暂时无法添加下级代理</option>
                                    <else/>
                                    <option value="0">-- 不代理此品牌 --</option>
                                </empty>
                                <foreach name="ranks['ranks']" item="rank">
                                    <option value="{$rank['rank_id']}">
                                        {$rank['rank_name']}
                                    </option>
                                </foreach>
                            </select>
                        </div>
                    </notempty>
                </foreach>

            </div>
        </div>
        <div class="person-form">
            <span id="insert">添加</span>
            <span id="show" style="display: none">添加中....</span>
        </div>
    <!--</form>-->

    <script type="text/javascript">
        $(function(){
            $("#insert").click(function() {
                var agent_name = $("#agent_name").val();
                var mobile = $("#mobile").val();
                if (!agent_name) {
                    layer.alert("请填写姓名");
                    return false;
                }
                var mobilePattern = /^0?1[3|4|5|8|7][0-9]\d{8}$/;
                if (mobile == '' || !mobilePattern.test(mobile)) {
                    layer.alert('请填写正确手机号码！');
                    return false;
                }

                $("#insert").hide();
                $("#show").show();

                $("#agent_add_form").submit();
                var brand_list = [];
                for ($i = 0; $i < $(".hidden_brand").length; $i++) {
                    brand_list[$i] = $(".hidden_brand").eq($i).val();
                }
                var rank_list = [];
                for ($a = 0; $a < $(".ranks").length; $a++) {
                    rank_list[$a] = $(".ranks").eq($a).val();
                }
                $.ajax({
                    url: "{:U('Agent/agent_add')}",
                    type: "post",
                    data: {
                        agent_name: agent_name,
                        mobile: mobile,
                        brand_id: brand_list,
                        rank_id: rank_list
                    },
                    success: function (msg) {
                        if (msg == 1) {
                            layer.msg("添加成功", {icon: 1, time: 500});
                            setTimeout(function() {
                                window.location.href = "{:U('Agent/index')}";
                            }, 500);
                        } else if (msg == -1) {
                            layer.msg("请选择代理等级", {icon: 2, time: 1000})
                            $("#insert").show();
                            $("#show").hide();
                        } else if (msg == -2) {
                            layer.msg("请填写正确的手机号码", {icon: 2, time: 1000})
                            $("#insert").show();
                            $("#show").hide();
                        } else if (msg == -3) {
                            layer.msg("已存在此手机号码", {icon: 2, time: 1000})
                            $("#insert").show();
                            $("#show").hide();
                        } else if (msg == -4) {
                            layer.msg("请求有误", {icon: 2, time: 1000})
                            $("#insert").show();
                            $("#show").hide();
                        } else if (msg == -5) {
                            layer.msg("已有重复数据", {icon: 2, time: 1000})
                            $("#insert").show();
                            $("#show").hide();
                        }
                    }
                });
            });
        });
    </script>
<include file="common:footer"/>
